<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Request;
class About extends Controller
{

    public function index()
    {
        $this->fetch('about/about');
    }



    //    编辑关于我们
    public function edit()
    {


        $id = input('id');
        $about = db('to_about')->find($id);

        $title= $about['title'];
        $desc = $about['desc'];

        return $this->fetch('about/edit', [
            'id' => $id,
            'title' => $title,
            'desc' => $desc

        ]);


    }

    public function aboutedits(){
        $id = input('id');
        // dump($id);die;
//        $links = db('to_about')->find($id);

        if (request()->isPost()) {
            $data = [
                'id' => $id,
                'title' => input('title'),
                'desc' => input('desc')
            ];


//            $validate = \think\Loader::validate('Links');
//            if (!$validate->check($data)) {
//                $this->error($validate->getError());
//                die;
//            }
            if (db('to_about')->update($data)) {
                $this->success('修改关于成功',url('admin/about/about_select'));

            } else {
                $this->error('修改关于失败');
            }
            return;
        }
    }

    public function add()
    {
        return view('about/add');
    }

//查询关于我们
    public function about_select()
    {
        $list = Db::name('to_about')->field(['id', 'desc','title'])->select();

        return $this->fetch('about/about', [
            'list' => $list,
        ]);
    }




// 添加关于我们
    public function aboutadd()
    {


        if (request()->isPost()) {
            $data = [
                'title' => input('title'),
                'desc' => input('desc')
            ];


            if (db('to_about')->insert($data)) {
                return $this->success('添加关于我们成功', url('admin/about/about_select'));
            } else {
                return $this->error('添加关于我们失败');
            }
            return;
        }
    }


    // 删除关于我们
    public function del()
    {
        $id =input('id');

        if (db('to_about')->delete($id)) {
            $this->success('删除关于成功', 'admin/about/about_select');
        } else {
            $this->error('删除关于失败');
        }
        return;

        return $this->redirect('admin/about/about_select');
    }


}
